/**
 * onesalt.js, import common js & css files.
 */

__CreateJSPath = function (js) {
    var scripts = document.getElementsByTagName("script");
    var path = "";
    var l = scripts.length;
    for (var i = 0; i < l; i++) {
        var src = scripts[i].src;
        if (src.indexOf(js) != -1) {
            var ss = src.split(js);
            path = ss[0];
            break;
        }
    }
    var href = location.href;
    href = href.split("#")[0];
    href = href.split("?")[0];
    var ss = href.split("/");
    ss.length = ss.length - 1;
    href = ss.join("/");
    if (path.indexOf("https:") == -1 && path.indexOf("http:") == -1 && path.indexOf("file:") == -1 && path.indexOf("\/") != 0) {
        path = href + "/" + path;
    }
    if (path.charAt(path.length - 1) == '/') {
        path = path.substring(0, path.length - 1);
    }
    ss = path.split("/");
    ss.length = ss.length - 1;
    path = ss.join("/");
    if (path.charAt(path.length - 1) != '/') {
        path += "/";
    }
    return path;
}

if(onesaltPath === void 0) {
    var onesaltPath = __CreateJSPath("onesalt.js");

    // <!-- 新 Bootstrap 核心 CSS 文件 -->
    document.write('<link rel="stylesheet" href="' + onesaltPath + 'css/bootstrap.min.css">');

    // <!-- 可选的Bootstrap主题文件（一般不用引入） -->
    document.write('<link rel="stylesheet" href="' + onesaltPath + 'css/bootstrap-theme.min.css">');

    // <!-- jQuery文件。务必在bootstrap.min.js 之前引入 -->
    document.write('<script src="' + onesaltPath + 'js/jquery.js"></script>');

    // <!-- Underscore文件 -->
    document.write('<script src="' + onesaltPath + 'js/underscore.js"></script>');

    //<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    document.write('<script src="' + onesaltPath + 'js/bootstrap.min.js"></script>');

    //<!-- JSON解析 -->
    document.write('<script src="' + onesaltPath + 'js/json2.js"></script>');
    
    //<!-- Date time picker -->
    document.write('<link rel="stylesheet" href="' + onesaltPath + 'css/bootstrap-datetimepicker.min.css">');
    document.write('<script src="' + onesaltPath + 'js/bootstrap-datetimepicker.min.js" charset="UTF-8"></script>');
    document.write('<script src="' + onesaltPath + 'js/locales/bootstrap-datetimepicker.zh-CN.js" charset="UTF-8"></script>');
}

loadContent = function(url, params) {
  if(_.isString(url) && !_.isEmpty(url)) {
    $('#content').load(url, params, function(e) {
      $('#content').attr('url', url);
      $("#content a").click(function(e) {
        var href = $(this).attr("href");
        if(href != '#' && href.indexOf('://', 0) == -1) {
          loadContent(href);
          return false;
        }
        return true;
      });
    });
  }
}